package com.admin.controller;


import cn.hutool.json.JSONObject;
import com.admin.service.SMsService;
import com.admin.util.Result;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletRequest;
import java.util.Random;


@RestController
@ApiIgnore
/**
 * @ignore true
 */
public class SendSmsController {
    @Autowired
    private SMsService sendSmsService;

    @ApiOperation(value="短信接口", notes="短信接口")
    @GetMapping("/sendSms")
    public Result sendSms(String phone, HttpServletRequest request) {
        Result result = new Result();
        try{
            JSONObject json = new JSONObject();
            String code = String.valueOf(new Random().nextInt(899999) + 100000);
            boolean sendFlag = sendSmsService.sendSms(phone,code);
            if (sendFlag){
                json.append("code",code);
                request.getSession().setAttribute("code",code);
                result.setData(json);
                result.setMessage( Result.Code.SUCCESS,"发送成功");
            }else {
                result.setMessage( Result.Code.AUTHERROR,"发送短信失败");
            }
        }catch(Exception e){
            e.printStackTrace();
            result.setMessage( Result.Code.AUTHERROR,"发送短信失败");
        }
        return result;
    }
}
